package com.yixun.qfbao.response.customerpremises;

import com.yixun.qfbao.apidto.GetCPDetailResDto;
import com.yixun.qfbao.model.ReportVisit;
import com.yixun.qfbao.xf.model.FollowUpList;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesAffixDto;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.enums.CustomerStatusEnum;
import com.yixun.qfbao.xf.model.enums.ProcessStatusEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class ReportConvert {

    public static ReportDetailResDto convertDetail2ResDto(GetCPDetailResDto getCPDetailResDto, List<CustomerPremisesAffixDto> affixDtoList, Map<String, String> statusMap){
        ReportDetailResDto resDto = new ReportDetailResDto();
        if(Objects.isNull(getCPDetailResDto)){
            return null;
        }
        CustomerPremisesDto cusPreDto = getCPDetailResDto.getCpDto();
        if(Objects.isNull(cusPreDto)){
            return null;
        }

        ReportVisit reportVisit = getCPDetailResDto.getReportVisit();

        resDto.setId(cusPreDto.getId());
        resDto.setPremisesId(cusPreDto.getPremisesId());
        resDto.setPremisesName(cusPreDto.getPremisesName());
        resDto.setStoreName(cusPreDto.getStaffCompanyName());
        resDto.setAgencyName(cusPreDto.getStaffName());
        resDto.setAgencyPhone(cusPreDto.getStaffPhone());
        resDto.setCustomerName(cusPreDto.getName());
        resDto.setGender(cusPreDto.getGender());
        resDto.setGenderName(cusPreDto.getGenderCN());
        resDto.setMobile(cusPreDto.getMobile());
        resDto.setHopeVisitTime(cusPreDto.getHopeVisitTime());
        resDto.setVisitTime(cusPreDto.getVisitTime());
        resDto.setVisitBaohuTime(cusPreDto.getVisitBaohuTime());
        resDto.setReportPeriodTime(cusPreDto.getReportPeriodTime());
        resDto.setVisitCount(1);
        if(Objects.nonNull(reportVisit) && Objects.nonNull(reportVisit.getVisitCount())){
            resDto.setVisitCount(reportVisit.getVisitCount());
        }
        resDto.setCreateTime(cusPreDto.getCreateTime());
        resDto.setRemark(cusPreDto.getRemark());

        List<FollowUpList> followUpList = cusPreDto.getFulList();
        List<ReportFollow> follows = new ArrayList<>();
        for(FollowUpList followUp: followUpList){
            ReportFollow reportFollow = new ReportFollow();
            reportFollow.setFollowUser(followUp.getStaffName());
            reportFollow.setFollowContent(followUp.getRemark());
            reportFollow.setFollowTime(followUp.getCreateTime());
            List<String> urls = affixDtoList.stream().filter(item -> {
                Long followUpListId = item.getFollowUpListId();
                String fileType = item.getFileType();
                return Objects.nonNull(followUpListId) && followUpListId.equals(followUp.getId())
                        && (fileType.equalsIgnoreCase("JPG")
                        || fileType.equalsIgnoreCase("JPEG")
                        || fileType.equalsIgnoreCase("PNG")
                        || fileType.equalsIgnoreCase("SVG")
                        || fileType.equalsIgnoreCase("WEBP")
                        || fileType.equalsIgnoreCase("GIF"));
            })
                    .map(CustomerPremisesAffixDto::getUrl).collect(Collectors.toList());
            reportFollow.setFollowUrls(urls);
            follows.add(reportFollow);
        }
        resDto.setFollows(follows);

        int index = Integer.valueOf(statusMap.get("index"));
        int value = Integer.valueOf(statusMap.get("value"));
        ReportSteps reportSteps = new ReportSteps();
        reportSteps.setIndex(index);
        reportSteps.setValue(value);
        resDto.setSteps(reportSteps);

        return resDto;
    }
}
